/*================================================================
*   Copyright (C) 2020 Sangfor Ltd. All rights reserved.
*   
*   文件名称：test.cpp
*   创 建 者：Ann
*   创建日期：2020年06月08日
*   描    述：
*
================================================================*/

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>

int sockfd = -1;

void sigcb(int signo)
{
    printf("signo is [%d] form sigcb func\n", signo);
    //recvfrom
    char buf[1024] = { 0 };
    struct sockaddr_in peeraddr;
    socklen_t peeraddr_len = sizeof(struct sockaddr_in);
    recvfrom(sockfd, buf, sizeof(buf) - 1, 0, (struct sockaddr*)&peeraddr, &peeraddr_len);
}

int main()
{
    signal(SIGIO, sigcb);
    //SIGIO
    //1.如何去设置一个套接字，来处理SIGIO
    sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sockfd < 0)
    {
        perror("socket");
        return 0;
    }

    fcntl(sockfd, F_SETOWN, getpid());

    int flag = fcntl(sockfd, F_GETFL);
    fcntl(sockfd, F_SETFL, flag | O_ASYNC);

    //bind
    
    while(1)
    {
        sleep(1);
    }

    return 0;
}
